package com.yehuishou.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.BackItem;
import com.yehuishou.pojo.Classify;
import com.yehuishou.pojo.Sversion;
import com.yehuishou.pojo.Version;
import com.yehuishou.service.BackItemService;
import com.yehuishou.service.ClassifyService;
import com.yehuishou.service.IphoneService;


@Controller
public class IndexController {
	
	@Autowired
	private IphoneService iphoneService;
	
	@Autowired
	private BackItemService backItemService;
	
	@Autowired
	private ClassifyService classifyService;
	
	
	@RequestMapping("show1")
	@ResponseBody
    public Map<String, Object> index1(String npage,Integer cid,Integer pid){
		System.out.println("*********************************************************"+cid);
		 Map<String, Object> map = new HashMap<>();
			Integer page =0;
			if(npage!=null&&npage!=""){
				
				page = Integer.parseInt(npage);
			}
			
			
			
			
			
				List<Sversion>list2=iphoneService.selectByCids(cid);
				 pid=list2.get(0).getPid();
		
			
			PageHelper.startPage(page, 8);
			
			List<BackItem>sversions= backItemService.getBackItems(pid, cid);
			
			PageInfo<BackItem> pageInfo = new PageInfo<>(sversions);
			
			map.put("pageInfo",pageInfo);
			map.put("cid", cid);
			map.put("pid", pid);
			return map;
    }
	
	@RequestMapping("show2")
	@ResponseBody
	public Map<String, Object> index2(String npage,Integer cid,Integer pid){
		/*System.out.println("*********************************************************");*/
		 Map<String, Object> map = new HashMap<>();
			Integer page =0;
			if(npage!=null&&npage!=""){
				
				page = Integer.parseInt(npage);
			}
			List<Sversion>list2=iphoneService.selectByCids(cid);
			 pid=list2.get(0).getPid();
			PageHelper.startPage(page, 8);
			
			List<BackItem>sversions= backItemService.getBackItems(pid, cid);
			
			PageInfo<BackItem> pageInfo = new PageInfo<>(sversions);
			
			map.put("pageInfo",pageInfo);
			map.put("cid", cid);
			map.put("pid", pid);
			return map;
    }
	
	@RequestMapping("show3")
	@ResponseBody
		public Map<String, Object> index3(String npage,Integer cid,Integer pid){
			/*System.out.println("*********************************************************");*/
			 Map<String, Object> map = new HashMap<>();
				Integer page =0;
				if(npage!=null&&npage!=""){
					
					page = Integer.parseInt(npage);
				}
				
				List<Sversion>list2=iphoneService.selectByCids(cid);
				 pid=list2.get(0).getPid();
				
				PageHelper.startPage(page, 8);
				
				List<BackItem>sversions= backItemService.getBackItems(pid, cid);
				
				PageInfo<BackItem> pageInfo = new PageInfo<>(sversions);
				
				map.put("pageInfo",pageInfo);
				map.put("cid", cid);
				map.put("pid", pid);
				return map;
	    }
	@RequestMapping("show4")
	@ResponseBody
	public Map<String, Object> index4(String npage,Integer cid,Integer pid){
		/*System.out.println("*********************************************************");*/
		 Map<String, Object> map = new HashMap<>();
			Integer page =0;
			if(npage!=null&&npage!=""){
				
				page = Integer.parseInt(npage);
			}
			
			List<Sversion>list2=iphoneService.selectByCids(cid);
			 pid=list2.get(0).getPid();
			
			PageHelper.startPage(page, 8);
			
			List<BackItem>sversions= backItemService.getBackItems(pid, cid);
			
			PageInfo<BackItem> pageInfo = new PageInfo<>(sversions);
			
			map.put("pageInfo",pageInfo);
			map.put("cid", cid);
			map.put("pid", pid);
			return map;
    }
	@RequestMapping("show5")
	@ResponseBody
	public Map<String, Object> index5(String npage,Integer cid,Integer pid){
		/*System.out.println("*********************************************************");*/
		 Map<String, Object> map = new HashMap<>();
			Integer page =0;
			if(npage!=null&&npage!=""){
				
				page = Integer.parseInt(npage);
			}
			
			List<Sversion>list2=iphoneService.selectByCids(cid);
			 pid=list2.get(0).getPid();
			
			PageHelper.startPage(page, 8);
			
			List<BackItem>sversions= backItemService.getBackItems(pid, cid);
			
			PageInfo<BackItem> pageInfo = new PageInfo<>(sversions);
			
			map.put("pageInfo",pageInfo);
			map.put("cid", cid);
			map.put("pid", pid);
			return map;
    }
	@RequestMapping("showGalaxy")
	@ResponseBody
	public Map<String,Object> showIpad1(String npage,Integer pid,Integer cid){
		/*List<BackItem>sversions= backItemService.getBackItems(pid, cid);
		return sversions;*/
	    Map<String, Object> map = new HashMap<>();
			Integer page =0;
			if(npage!=null&&npage!=""){
				
				page = Integer.parseInt(npage);
			}
			
			
			PageHelper.startPage(page, 8);
			
			List<BackItem>sversions= backItemService.getBackItems(pid, cid);
			
			PageInfo<BackItem> pageInfo = new PageInfo<>(sversions);
			
			map.put("pageInfo",pageInfo);
			map.put("cid", cid);
			map.put("pid", pid);
			
			return map;
	}
	
	
	@RequestMapping("showbrand")
	@ResponseBody
    public  List<Version> index1(HttpServletRequest request,Integer cid){
		
		List<Version> list1= iphoneService.selectByCid();
		for (Version version : list1) {
			List<Sversion>list2=iphoneService.selectByCids(version.getCid());
			version.setList(list2);
			
		}
		
		
		return list1;
		 
  	  
    }
	
	
	@RequestMapping("showPager")
	@ResponseBody
	public Map<String,Object> showPager(String npage, Integer pid,Integer cid){
	    Map<String, Object> map = new HashMap<>();
		Integer page =0;
		if(npage!=null&&npage!=""){
			
			page = Integer.parseInt(npage);
		}
		
		
		PageHelper.startPage(page, 8);
		
		List<BackItem>sversions= backItemService.getBackItems(pid, cid);
		
		PageInfo<BackItem> pageInfo = new PageInfo<>(sversions);
		
		map.put("pageInfo",pageInfo);
		
		return map;
	}
	@RequestMapping("moreBrand")
	public String moreBrand(Integer cid,HttpServletRequest request){
		List<Sversion>moreBrand=iphoneService.selectByCids(cid);
		for (Sversion sversion : moreBrand) {
			sversion.setCid(cid);
		}
		//获取所有的品牌
				List<Classify> classifylist =classifyService.getAllClassify();
				request.setAttribute("classifylist", classifylist);
		        request.setAttribute("moreBrand", moreBrand);
		        request.setAttribute("cid", cid);
		        return "moreBrand";
	}

	
}
